<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Cart extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation'); //加载表单验证类库
        $this->load->helper('json');
        $this->load->model('cart_model');
    }

    /**
     * @SWG\Post(
     *     summary="购物车添加减少数量",
     *     path="/cart/changeCart",
     *     tags={"客户端"},
     *     operationId="21",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="类型（1.增加；2.减少）",
     *         in="formData",
     *         name="type",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Parameter(
     *         description="用户session_key",
     *         in="formData",
     *         name="session_key",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="店铺id",
     *         in="formData",
     *         name="shop_id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Parameter(
     *         description="商品id",
     *         in="formData",
     *         name="goods_id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function changeCart()
    {
        $post = $this->input->post();
        if ($post['session_key'] && $post['shop_id'] && $post['goods_id'] && $post['type']) {
            if (isset($post['num']) && $post['num'] != 0) {
                $post['num'] = $post['num'];
            } else {
                $post['num'] = "";
            }
            $result = $this->cart_model->changeCartNum($post);
            if ($result) {
                send_json(array('code' => 2000, 'data' => '', 'msg' => "操作成功"));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "操作失败"));
            }
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Post(
     *     summary="删除购物车",
     *     path="/cart/delCart",
     *     tags={"客户端"},
     *     operationId="22",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="商品id",
     *         in="formData",
     *         name="goods_id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Parameter(
     *         description="用户session_key",
     *         in="formData",
     *         name="session_key",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function delCart()
    {
        $post = $this->input->post();
        if ($post['session_key'] && $post['goods_id']) {
            $result = $this->cart_model->delCartNum($post);
            if ($result) {
                send_json(array('code' => 2000, 'data' => '', 'msg' => "操作成功"));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "操作失败"));
            }
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }
}
